/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListPanel.java
 *
 * Created on Mar 1, 2013, 1:17:58 PM
 */
package edu.ncsu.csc573.project1.gui;

import edu.ncsu.csc573.project1.peer.Peer;
import edu.ncsu.csc573.project1.util.RfcField;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author user
 */
public class ListPanel extends javax.swing.JPanel {

    Peer peer;
    /** Creates new form ListPanel */
    public ListPanel(Peer peer) {
        this.peer = peer;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fetchButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        rfcListTable = new javax.swing.JTable() {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        fetchButton.setText("List available RFCs");
        fetchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fetchButtonActionPerformed(evt);
            }
        });

        rfcListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "RFC Number", "RFC Title", "Host Name", "Upload Port"
            }
        ));
        jScrollPane1.setViewportView(rfcListTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(fetchButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(fetchButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void fetchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fetchButtonActionPerformed

        LinkedList<RfcField> rfcList = peer.list();
        DefaultTableModel rfcTableModel = (DefaultTableModel) this.rfcListTable.getModel();
      
        if (rfcList != null) { 
            rfcTableModel.getDataVector().removeAllElements();
            for (int i = 0; i < rfcList.size(); ++i) {
                RfcField currentField = rfcList.get(i);
                rfcTableModel.addRow(new Object[]{new Integer(currentField.getRfcNumber()), currentField.getRfcTitle(), currentField.getHostName(), currentField.getUploadPortNumber()});
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_fetchButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton fetchButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable rfcListTable;
    // End of variables declaration//GEN-END:variables
}
